package cci;

import cdp.Relatorio;
import cgd.RelatorioDao;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet()
public abstract class Controller extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher cih;
        String evento = request.getParameter("evento");
        if (evento == null) {
            String mensagem = "Pagina nao encontrada.";
            cih = request.getRequestDispatcher("modulos/admin/index.jsp?mensagem=" + mensagem);
            cih.forward(request, response);
            return;
        }
        try {
            if (evento.equals("incluir")) {
                addParametros(request);
            } else if (evento.equals("alterar")) {
                request.setAttribute(getTipoControlador(), montar(request));
            } else if (evento.equals("consultar")) {
                request.setAttribute("lista", montarLista());
            } else if (evento.equals("enviarEmails")) {
                getEnviarEmails(request);
                cih = request.getRequestDispatcher(
                        "modulos/admin/acao/" + evento + ".jsp");
                cih.forward(request, response);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        cih = request.getRequestDispatcher(
                "modulos/admin/" + getTipoControlador() + "/" + evento + ".jsp");
        cih.forward(request, response);
        return;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher cih;
        String mensagem = "";
        String evento = request.getParameter("evento");
        try {
            if (evento.equals("incluir") || evento.equals("alterar")) {
                mensagem = inserirOuAlterar(request);
            } else if (evento.equals("excluir")) {
                mensagem = excluir(request);
            } else if (evento.equals("enviarEmails")) {
                List<Relatorio> listaRelatorios = RelatorioDao.getInstance().obterTodos();
                request.setAttribute("listaRelatorios", listaRelatorios);
                cih = request.getRequestDispatcher(
                        "modulos/admin/acao/xml.jsp");
                cih.forward(request, response);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("admin.jsp?mensagem=" + mensagem);
        return;

    }

    protected abstract Object montar(HttpServletRequest request) throws Exception;

    protected abstract String inserirOuAlterar(HttpServletRequest request) throws Exception;

    protected abstract String excluir(HttpServletRequest request) throws Exception;

    protected abstract Object montarLista() throws Exception;

    protected abstract String getTipoControlador();

    protected abstract void addParametros(HttpServletRequest request);

    protected void getEnviarEmails(HttpServletRequest request) {
    }
}
